var gempCode="";
var growCount="";
var newAddedRowsCounter = 0;
var tempCtrFlag=true;

function addTimesheetRow(empCode){ 
rowCount=document.getElementById(empCode+"_no_of_rows").value;
empName=document.getElementById(empCode+"_name").value;
growCount=rowCount;
gempCode = empCode;
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)  {
  alert ("Your browser does not support AJAX!");
  revertSubmit();
  return;
  } 
var url="timesheet.html?method=addTimesheetRow&emp_code="+empCode+"&rowCount="+rowCount+"&emp_name="+empName;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function revertSubmit(){
  document.getElementById("Submit").disabled=false;
  document.getElementById("Submit").value="Submit";
}


function stateChanged(){ 
	if (xmlHttp.readyState==4)	{ 
		document.getElementById(gempCode+"_timesheetRow").style.display = 'block';
		document.getElementById(gempCode+"_timesheetRow").style.display = 'block'; 
		document.getElementById(gempCode+"_timesheetRow").innerHTML = xmlHttp.responseText;
		document.getElementById(gempCode+"_no_of_rows").value = parseInt(growCount)+1;
	}
}

function removeTimesheetRow(d){
growCount = parseInt(growCount)-1;
document.getElementById(d).style.display = 'none';	
	if(growCount==0){
		document.getElementById(gempCode+"_no_of_rows").value = 1;
	}else{		
		document.getElementById(gempCode+"_no_of_rows").value = growCount;
	}
}

function validateEmpty(fld,name) { 
var error = "";
if(fld != null) {
	if (fld.value.length == 0) {
		fld.style.background = 'Yellow'; 
		error = name+" field has not been filled in.\n";
	} else {
		var numaric = fld.value;
		for(var j=0; j<numaric.length; j++)
		{
			if(j==0){
			var alphaa = numaric.charAt(j);
			var chCode = alphaa.charCodeAt(0);
			if(chCode == "32"){
				fld.style.background = 'Yellow'; 
				error = name+" should not start with space.\n";
			}
			}
		}
		//fld.style.background = 'White';
	}
}
return error;  
}


function validateFieldMaxLength(fld,name,maxLength ) { 
var error = "";

if (fld.value.length > maxLength) {
	fld.style.background = 'Yellow'; 
	error = name+" field has more than "+maxLength+" characters. \n";
	revertSubmit();
}
return error;  
}


function validateSpecialChar(fld,name){ 
var error = "";
if(fld != null) {
var iChars = "!@#$%^&*()+=[]\\\';./{}|\":<>?_";
  for (var i = 0; i < fld.value.length; i++) {
  	if (iChars.indexOf(fld.value.charAt(i)) != -1) {
  	fld.style.background = 'Yellow';
	error = name+" field has special characters.\n";
	revertSubmit();
	return error;
  	}
  }
}
  return error;
}

function validateInteger(fld,name){ 
var error = "";
   	if (fld != null && isNaN(fld.value)){
  	fld.style.background = 'Yellow';
	error = name+" field should be number.\n";
	revertSubmit();
	
  	}
   	else if(fld != null && fld.value < 0){
		fld.style.background = 'Yellow';
		error = name+" field should not be negative.\n";	  	
		revertSubmit();
	}
 
  return error;
}
function validateInteger1(fld,name){ 
var error = "";
var iChars = "!@#$%^&*()+=-[]\\\';,/{}|\":<>?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
  for (var i = 0; i < fld.value.length; i++) {
  	if (iChars.indexOf(fld.value.charAt(i)) != -1) {
  	fld.style.background = 'Yellow';
	error = name+" field should be number.\n";
	revertSubmit();
	return error;
  	}
  }
  return error;
}

function confirmCopyFromPreviousWeek(){
    document.copyform.calender_date.value = document.timesheet.calender.value;
	if(confirm('Timesheet data for selected week will be deleted. Please confirm to delete & continue?')){
		var opts = document.getElementById('chooseProject').options;
		var prjId="";
		for(i=0;i<opts.length;i++) {
			if(opts[i].selected) {
				prjId =  opts[i].value;	
			}
		}
		var pmId="", pmIdStr="";
		if(document.getElementById('pmId') != null) {
			var optns = document.getElementById('pmId').options;
			for(i=0;i<optns.length;i++) {
				if(optns[i].selected) {
					pmId =  optns[i].value;	
				}
			}
		}
		if(pmId != null && pmId=="") {
			pmIdStr="";
		} else {
			pmIdStr = "&&pmId="+pmId;
		}
		document.forms.copyform.action = "timesheet.html?method=copyTimesheet&&chooseProject="+prjId+pmIdStr;
		document.forms.copyform.submit();
	}
}

function validateFormOnSubmit(){
	var reason = "";
 	var i="";
	var sizeOfList = document.getElementById('sizeOfCounter').value;
	
	for(i=1;i<=sizeOfList;i++){
		if(document.getElementById('name'+i) != null) {
			var name = document.getElementById('name'+i).value;
			var monHoursName = name + " Mon Row";
			if(document.getElementById('monHours'+i) != null) {
				monHours = document.getElementById('monHours'+i);
				var monHoursEmp  = validateEmpty(monHours,monHoursName);
				reason += monHoursEmp;
				var monHoursSpl  = validateInteger(monHours,monHoursName);
				reason += monHoursSpl;
			}
			if(document.getElementById('tuesHours'+i) != null) { 
				var tuesHoursName = name + " Tue Row";
				tuesHours = document.getElementById('tuesHours'+i);
				var tuesHoursEmp  = validateEmpty(tuesHours,tuesHoursName);
				reason += tuesHoursEmp;
				var tuesHoursSpl  = validateInteger(tuesHours,tuesHoursName);
				reason += tuesHoursSpl;
			}
			if(document.getElementById('wedHours'+i) != null) {
				var wedHoursName = name + " Wed Row";
				wedHours = document.getElementById('wedHours'+i);
				var wedHoursEmp  = validateEmpty(wedHours,wedHoursName);
				reason += wedHoursEmp;
				var wedHoursSpl  = validateInteger(wedHours,wedHoursName);
				reason += wedHoursSpl;
			}
			if(document.getElementById('thursHours'+i) != null) {
				var thursHoursName = name + " Thur Row";
				thursHours = document.getElementById('thursHours'+i);
				var thursHoursEmp  = validateEmpty(thursHours,thursHoursName);
				reason += thursHoursEmp;
				var thursHoursSpl  = validateInteger(thursHours,thursHoursName);
				reason += thursHoursSpl;
			}
			if(document.getElementById('friHours'+i) != null) {
				var friHoursName = name + " Fri Row";
				friHours = document.getElementById('friHours'+i);
				var friHoursEmp  = validateEmpty(friHours,friHoursName);
				reason += friHoursEmp;
				var friHoursSpl  = validateInteger(friHours,friHoursName);
				reason += friHoursSpl;
			}
			if(document.getElementById('satHours'+i) != null) {
				var satHoursName = name + " Sat Row";
				satHours = document.getElementById('satHours'+i);
				var satHoursEmp  = validateEmpty(satHours,satHoursName);
				reason += satHoursEmp;
				var satHoursSpl  = validateInteger(satHours,satHoursName);
				reason += satHoursSpl;
			}
			if(document.getElementById('sunHours'+i) != null) {
				var sunHoursName = name + " Sun Row";
				sunHours = document.getElementById('sunHours'+i);
				var sunHoursEmp  = validateEmpty(sunHours,sunHoursName);
				reason += sunHoursEmp;
				var sunHoursSpl  = validateInteger(sunHours,sunHoursName);
				reason += sunHoursSpl;
			}
		}
	}
	//Start Here Eswar
	var j=0;
	var empActivityArray = new Array();
	var empOnsiteArray = new Array();
	var empCodeArray = new Array();
	var counterArray = new Array();
	var empCode=0, activity=0, onsite=0;
	var cnt = parseInt(document.getElementById('sizeOfCounter').value)+1;
	var addCtr=0;
	
	for(addCtr=cnt; addCtr>0; addCtr--) {
		if(document.getElementById('empCode'+addCtr) != null) {
			empCode = document.getElementById('empCode'+addCtr).value;
			activity = document.getElementById('activity'+addCtr).value;
		    onsite = document.getElementById('onsite'+addCtr).checked;
			for(i=1; i<=sizeOfList; i++) {
				if(document.getElementById('empCode'+i) != null) {
					empCodeArray[j] = document.getElementById('empCode'+i).value;
					if(empCode == empCodeArray[j]) {
						counterArray[j] = i;			
						j++;
					}
				}
			}	
			
			for(i=0; i<counterArray.length; i++) {
				if(document.getElementById('activity'+counterArray[i]) != null && document.getElementById('onsite'+counterArray[i]) != null) {
					empActivityArray[i] = document.getElementById('activity'+counterArray[i]).value;
					empOnsiteArray[i] = document.getElementById('onsite'+counterArray[i]).checked;
					if(empActivityArray[i]=="-1" || empActivityArray[i] == -1) {
						reason= "Please select a valid activity type.";
						revertSubmit();
						document.getElementById('activity'+counterArray[i]).focus();
						break;
					}
					//if(addCtr != counterArray[i]) {
					if(activity.length <1 && empActivityArray[i].length <1) {
							reason= "Please select any activity.";
							revertSubmit();
							break;
					}
					//}
				}
			}
		}
	}
	
	if (reason != "") {
		 alert("Errors:\n" + reason); 
		 revertSubmit();
		return false;
	} 
	var opts = document.getElementById('chooseProject').options;
	var prjId="";
	for(i=0;i<opts.length;i++) {
		if(opts[i].selected) {
			prjId =  opts[i].value;	
		}
	}
	var suprId="", suprStr="";
	if(document.getElementById('pmId') != null) {
		var opts1 = document.getElementById('pmId').options;
		for(i=0;i<opts1.length;i++) {
			if(opts1[i].selected) {
				suprId =  opts1[i].value;	
			}
		}
	}
	if(suprId!=null && suprId !="") {
		suprStr = "&&pmId="+suprId;
	}
	document.forms.timesheet.action="timesheet.html?method=saveTimeSheet&&chooseProject="+prjId+suprStr;
	document.forms.timesheet.submit();
}

function totalHours(i,Hours){
	document.getElementById('totalHours'+i).value =Number(document.getElementById('monHours'+i).value) + Number(document.getElementById('tuesHours'+i).value)  + Number(document.getElementById('wedHours'+i).value) + Number(document.getElementById('thursHours'+i).value) + Number(document.getElementById('friHours'+i).value) + Number(document.getElementById('satHours'+i).value) + Number(document.getElementById('sunHours'+i).value);
}

function totalHoursOnLoad(i){
	var monNum = 0, tueNum=0, wedNum=0, thuNum=0, friNum=0, satNum=0, sunNum=0;
	if(document.getElementById('monHours'+i) != null) {
		monNum= Number(document.getElementById('monHours'+i).value);
	}
	if(document.getElementById('tuesHours'+i) != null) {
		tueNum = Number(document.getElementById('tuesHours'+i).value);
	}
	if(document.getElementById('wedHours'+i) != null) {
		wedNum = Number(document.getElementById('wedHours'+i).value);
	}
	if(document.getElementById('thursHours'+i) != null) {
		thuNum = Number(document.getElementById('thursHours'+i).value);
	}
	if(document.getElementById('friHours'+i) != null) {
		friNum = Number(document.getElementById('friHours'+i).value);
	}
	if(document.getElementById('satHours'+i) != null) {
		satNum = Number(document.getElementById('satHours'+i).value);
	}
	if(document.getElementById('sunHours'+i) != null) {
		sunNum = Number(document.getElementById('sunHours'+i).value);
	}
	
	if(document.getElementById('totalHours'+i) != null) {
		document.getElementById('totalHours'+i).value = monNum + tueNum  + wedNum + thuNum + friNum + satNum + sunNum ;
	}
}

function totalhoursOnload(){
	var i="";
	var sizeOfList = document.getElementById('sizeOfList').value;
	for(i=1;i<=sizeOfList;i++){
		totalHoursOnLoad(i);
	}
}


function IsNumeric(elem) {
	var sText = elem.value;
   var ValidChars = "0123456789.";
   var dotOccur=0;
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if(Char=='.' || Char ==".") {
    	  dotOccur ++;
      }
      if (ValidChars.indexOf(Char) == -1 || dotOccur >1)  {
         IsNumber = false;
         }
      }
   //var regex = "^\d*\.{0,1}\d*?$";
   
   return IsNumber;
   
 }


function checkLessThan24(elem) {
	if(elem.value>24){
		alert('Value should be less than or equal to 24 hours.');
		revertSubmit();
		elem.value=0.0;
	}
	
	if (elem.value < 0 || isNaN(elem.value)){
		alert('Please enter valid data'); 
		revertSubmit();
		elem.value=0.0;
	}
}
var activityArray = new Array();
function addRow(tableName,EmpCode,Name, mnd, tusd, wdns, thus, frdy, satu, sundy, maincnd, activityString){
	var mndStrDisable="",mndStr="", tusdStr="", wdnsStr="", thusStr="", frdyStr="",satuStr="",sundyStr="";
	if(activityArray.length == 0){
		activityArray = activityString.split(",");
//		for(z=0;stringArray.length>1;z++){
//			var index = stringArray.indexOf(",");
//			tempArr[z] = stringArray.substring(0, index);
//			index++;
//			stringArray = stringArray.substring(index, stringArray.length);
//		}
	}
	
	if(maincnd=="true" || maincnd==true) {
		mndStrDisable = "disabled='disabled'";
	}
	if(mnd=="true" || mnd==true) {
		mndStr="readonly='true' style='background:#C0C0C0;'";
	}
	if(tusd=="true" || tusd==true) {
		tusdStr="readonly='true' style='background:#C0C0C0;'";
	}
	if(wdns=="true" || wdns==true) {
		wdnsStr="readonly='true' style='background:#C0C0C0;'";
	}
	if(thus=="true" || thus==true) {
		thusStr="readonly='true' style='background:#C0C0C0;'";
	}
	if(frdy=="true" || frdy==true) {
		frdyStr="readonly='true' style='background:#C0C0C0;'";
	}
	if(satu=="true" || satu==true) {
		satuStr="readonly='true' style='background:#C0C0C0;'";
	}
	if(sundy=="true" || sundy==true) {
		sundyStr="readonly='true' style='background:#C0C0C0;'";
	}
	newAddedRowsCounter++;
	var tbody = document.getElementById(tableName).getElementsByTagName("TBODY")[0];
	var row = document.createElement("TR");
	var rowNo = tbody.rows.length;
	var currRowNum = parseInt(rowNo ) ;
	var counter = parseInt(document.getElementById('sizeOfCounter').value)+1;
	document.getElementById('sizeOfCounter').value=counter;
	
	var row = tbody.insertRow(currRowNum);
	row.id = EmpCode+currRowNum+"_row";
	var tda = document.createElement("TD");	
	var strHtml0 = "<span id='"+EmpCode+currRowNum+"_remove' style='align:left;'><a href='#' class='bignum' style='color:#ff00ff;' onclick=javascript:removeRow('"+tableName+"','"+EmpCode+"','"+currRowNum+"');>Remove</a></span>";	
	
	tda.innerHTML = strHtml0;

	var td1 = document.createElement("TD");
       
    var strHtml1 = "  <input type='hidden' id='empCode"+counter+"' name='empCode"+counter+"' value='"+EmpCode+"' /><input type='text' readonly='true' value='"+Name+"'  id='name"+counter+"'  name='name"+counter+"' size='20'/>";
    td1.innerHTML = strHtml1;  

    var td2 = document.createElement("TD");
    var strHtml2 = "<input onblur='javascript:checkLessThan24(this);totalHours("+counter+");' type='text'  value='0.0'  name='monHours"+counter+"'  id='monHours"+counter+"' size='3' maxlength='5' "+mndStr+" />";
    td2.innerHTML = strHtml2; 

    var td3 = document.createElement("TD");
    var strHtml3 = "<input onblur='javascript:checkLessThan24(this);totalHours("+counter+");' type='text'  value='0.0'  name='tuesHours"+counter+"' id='tuesHours"+counter+"' size='3' maxlength='5'   "+tusdStr+" />";
    td3.innerHTML = strHtml3;

    var td4 = document.createElement("TD");	            
    var strHtml4 = "<input onblur='javascript:checkLessThan24(this);totalHours("+counter+");' type='text' value='0.0'  name='wedHours"+counter+"' id='wedHours"+counter+"' size='3' maxlength='5'  "+wdnsStr+"  />";
    td4.innerHTML = strHtml4;

	var td5 = document.createElement("TD");
    var strHtml5 = "<input onblur='javascript:checkLessThan24(this);totalHours("+counter+");' type='text' value='0.0'  name='thursHours"+counter+"' id='thursHours"+counter+"' size='3' maxlength='5'   "+thusStr+" />";
    td5.innerHTML = strHtml5;

   var td6 = document.createElement("TD");
    var strHtml6 = "<input onblur='javascript:checkLessThan24(this);totalHours("+counter+");' type='text' value='0.0'  name='friHours"+counter+"' id='friHours"+counter+"' size='3' maxlength='5'   "+frdyStr+" />";
    td6.innerHTML = strHtml6;


    var td7 = document.createElement("TD");
    var strHtml7 = "<input onblur='javascript:checkLessThan24(this);totalHours("+counter+");' type='text' value='0.0'  name='satHours"+counter+"' id='satHours"+counter+"' size='3' maxlength='5'   "+satuStr+" />";
    td7.innerHTML = strHtml7;


    var td8 = document.createElement("TD");
    var strHtml8 = "<input onblur='javascript:checkLessThan24(this);totalHours("+counter+");' type='text' value='0.0'  name='sunHours"+counter+"' id='sunHours"+counter+"' size='3' maxlength='5'  "+sundyStr+" />";
    td8.innerHTML = strHtml8;

    var tdtot = document.createElement("TD");
    var strHtmltot = "<input  type='text' readonly='true' id='totalHours"+counter+"' value='0' name='totalHours"+counter+"' size='3'/>";
    tdtot.innerHTML = strHtmltot;

    var td9 = document.createElement("TD");
    td9.setAttribute("align","left");
	 var strHtmlTemp="";
     var finalOptemp="";
     strHtmlTemp +="<option value='-1' >Select</option>";
	 for(x=0; x<activityArray.length; x++)
	  {        
	            strHtmlTemp +="<option value='"+activityArray[x]+"' >"+activityArray[++x]+"</option>";
				
	  }
	  finalOptemp = strHtmlTemp;
	  var strHtml9 = "<select onChange='javascript:validateRow("+counter+")' id='activity"+counter+"' name='activity"+counter+"' "+mndStrDisable+" >+"+finalOptemp+"+</select>";
	
	  td9.innerHTML = strHtml9;


/*
	  var td10 = document.createElement("TD")	
	  var strHtml10 = "<input type='text' onBlur='javascript:validateRow("+counter+")' value='' name='projectCode"+counter+"' id='projectCode"+counter+"' size='20' maxlength='20'/>";
	  td10.innerHTML = strHtml10

*/

	  var td11 = document.createElement("TD");
	  td11.setAttribute("align","right");
	  td11.setAttribute("width","50");
	  var strHtml11 = "<input name='onsite"+counter+"' onClick='javascript:validateRow("+counter+")' id='onsite"+counter+"'  value='1' type='checkbox' "+mndStrDisable+" />";
	  td11.innerHTML = strHtml11;
	   
	  	row.appendChild(tda);
	    row.appendChild(td1);
	    row.appendChild(td2);
	    row.appendChild(td3);
	    row.appendChild(td4);
	    row.appendChild(td5);
	    row.appendChild(td6);
	    row.appendChild(td7);
	    row.appendChild(td8);
		row.appendChild(tdtot);
	    row.appendChild(td9);
	   // row.appendChild(td10);
	    row.appendChild(td11);
		//updateRowCounters(tbody);
  }

function updateRowCounters(tbody){
  var rowCount = tbody.rows.length;
    for (var currentRow = 2; currentRow < rowCount; currentRow++) {
        tbody.rows[currentRow].id = currentRow;		
    } 
}

function removeRow(tableName, EmpCode, currRowNum) {
	if(newAddedRowsCounter>0){
	   newAddedRowsCounter--;
   }
    var tbody = document.getElementById(tableName).getElementsByTagName("TBODY")[0];
    var counter = parseInt(document.getElementById('sizeOfCounter').value)-1;
	document.getElementById('sizeOfCounter').value=counter;
	var row_tr = document.getElementById(EmpCode+currRowNum+"_row");
	tbody.removeChild(row_tr);
}

String.prototype.trim = function () {
    return this.replace(/^\s* /, "").replace(/\s*$/, "");
};

function submitSelectedSupervisor(theForm){
	var selPmId;
	if(document.getElementById('pmId') != null) {
		var chopt = document.getElementById('pmId').options;
		for(i=0;i<chopt.length;i++) {
			if(chopt[i].selected) {
				selPmId = chopt[i].value;
			}
		}
	}
	if(selPmId.value==-1) {
		alert("Please select a valid Supervisor.");
		revertSubmit();
		return false;
	} else {
		document.forms.timesheet.submit();
	}
 }

function validateRow(ctr){	
    var i, j=0;
    var addedCtr= ctr;
	sizeOfList = document.getElementById('sizeOfCounter').value;

	var empActivityArray = new Array();
	//var empProjectCodeArray = new Array();
	var empOnsiteArray = new Array();
	var empCodeArray = new Array();

	var counterArray = new Array();

	if (document.getElementById('empCode' + addedCtr) != null
			&& document.getElementById('activity' + addedCtr) != null
			&& document.getElementById('onsite' + addedCtr) != null) {
		var empCode = document.getElementById('empCode'+addedCtr).value;
		//var projectCode = document.getElementById('projectCode'+addedCtr).value.toUpperCase().trim();
	    var activity = document.getElementById('activity'+addedCtr).value;
	    var onsite = document.getElementById('onsite'+addedCtr).checked;
	
		for(i=1; i<=sizeOfList; i++){
			empCodeArray[j] = document.getElementById('empCode'+i).value;
			if(empCode == empCodeArray[j])
			{
				counterArray[j] = i;			
				j++;
			}	
		}	
		
		for(i=0; i<counterArray.length; i++)
		{
			empActivityArray[i] = document.getElementById('activity'+counterArray[i]).value;
			//empProjectCodeArray[i] = document.getElementById('projectCode'+counterArray[i]).value.toUpperCase().trim();
	        empOnsiteArray[i] = document.getElementById('onsite'+counterArray[i]).checked;
	
			if(addedCtr != counterArray[i])
				{
					if((activity == empActivityArray[i]) /*&& (projectCode == empProjectCodeArray[i])*/ && (onsite == empOnsiteArray[i]) )
					{					
							alert("Same combination already exist in previous rows" +
									". please change 'Activity' or 'Onsite' combinations");
							//document.getElementById('projectCode'+addedCtr).value ='';
							document.getElementById('onsite'+addedCtr).checked = false;
							document.getElementById('activity'+addedCtr).value = "-1";
							revertSubmit();
							break;
					}
				}
			if(empActivityArray[i] == -1 || empActivityArray[i] == "-1") {
				alert("Please select a valid activity type");
				document.getElementById('activity'+addedCtr).focus();
				revertSubmit();
				break;
			}
		}
	}
}

/*

function assignActivity()
{
		<%for (int i = 0, j = 0; i < activityList.size(); i++, j++) {%>
           	tempArr[<%=j%>] ='<%=activityList.get(i).getId()%>'; 
			tempArr[<%=++j%>] ='<%=activityList.get(i).getActivityName()%>'; 
       	<%}%>
}
*/

function initialize(weekStartDate){
	updateWeek(weekStartDate);
	totalhoursOnload();
//	assignActivity();
}


function validateProjectDetailsAndSubmit(){
	var suprId="";
	if(document.getElementById('pmId') != null) {
		var opts1 = document.getElementById('pmId').options;
		for(i=0;i<opts1.length;i++) {
			if(opts1[i].selected) {
				suprId =  opts1[i].value;	
			}
		}
	}
	
	var opts = document.getElementById('chooseProject').options;
	var prjId="";
	for(i=0;i<opts.length;i++) {
		if(opts[i].selected) {
			prjId =  opts[i].value;	
		}
	}
	if(prjId==-1) {
		alert("Please select a valid project");
		resetSubmit();
		return false;
	} else {
		if(confirm('Any unsaved timesheet data for selected week will be cleared. Press [OK] to confirm & continue?')){
			var supervisorStr = "";
			if(suprId=="" || suprId==null) {
				supervisorStr = "";
			} else {
				supervisorStr= "&&pmId="+suprId;
			}
			document.forms.populatedataform.action="timesheet.html?method=populateTimesheet&&chooseProject="+prjId+supervisorStr;
			document.forms.populatedataform.submit();
		} else {
			if(document.getElementById("selectedProjectIdOnChange") != null ) {
				var oldVal = document.getElementById("selectedProjectIdOnChange").value;
				var op11 = document.getElementById('chooseProject').options;
				for(i=0;i<op11.length;i++) {
					if(oldVal!=-1 && oldVal == opts[i].value) { 
						op11[i].selected="selected";	
					}
				}
			}
		}
	}
}

function checkNewRow() {
	var tmpCtrol =parseInt(document.getElementById('sizeOfCounter').value);
	validateRow(tmpCtrol);
}
var superviId = 0;

function validateSupervisorChange() {
	var opts = document.getElementById("pmId").options;
	var pMgrId=0;
	for(i=0; i < opts.length;i++) {
		if(opts[i].selected) {
			pMgrId = opts[i].value;
			break;
		}
	}
	if(document.getElementById('chooseProject') != null) {
		var optt = document.getElementById('chooseProject').options;
		for(i=0; i < optt.length;i++) {
			if(optt[i].value==-1) {
				optt[i].selected="selected";
				break;
			}
		}
	}
	if(pMgrId == -1) {
		alert("Please select a valid Manager");
		resetSubmit();
	} else {
		document.forms.timesheet.action="timesheet.html?method=populateTimesheet"; 
		document.forms.timesheet.submit();
	}
}
function cDate(nd) {
	// this is the symbol that separates months, days and years and you can set
	// it to anything, just pick your separator
	var separator = "/";

	// puts mm, dd and yyyy into an array
	var ndarr = nd.split(separator);

	// saves each argument of the array into a string starting with the day
	nd = ndarr[1] + separator + ndarr[0] + separator + ndarr[2];
	return nd;
}
function chkUndefined(nd) {
	var separator = "/";
	if(nd == "undefined") {
		return true;
	} else {
		var ndarr = nd.split(separator);
		if(ndarr[0] == "undefined" || ndarr[1] == "undefined" || ndarr[2] == "undefined") {
			return true;
		} else {
			return false;
		}
	}
}
function validateDateBeforeSubmit(currentDate,pStartDate,pEndDate,weekFridayDate, selectedPM, selectedProjectTitle) {
	document.getElementById("Submit").disabled=true;
	document.getElementById("Submit").value="Processing";
	var dateSelected  = document.getElementById("calender").value;
	var selDate = cDate(dateSelected);
	var wkPrjEndDate,wkPrjStartDate;
	var frmServerDate = cDate(currentDate);
	var submitOrNot=true;
	if( null != pEndDate) {
		wkPrjEndDate = cDate(pEndDate);
	}
	if( null != pStartDate) {
		wkPrjStartDate = cDate(pStartDate);
	}
	var wkFridayDate = cDate(weekFridayDate);
	if(((Date.parse(selDate) <= Date.parse(frmServerDate)) && Date.parse(frmServerDate) < Date.parse(wkFridayDate)) &&
			(Date.parse(wkPrjStartDate) < Date.parse(selDate)) && (wkPrjEndDate == null || chkUndefined(wkPrjEndDate) || (wkPrjEndDate != null && (Date.parse(frmServerDate) < Date.parse(wkPrjEndDate) || Date.parse(wkPrjEndDate) > Date.parse(wkFridayDate))))) {
		return timesheetCannotSubmit();
	} 

	if(((Date.parse(selDate) < Date.parse(frmServerDate)) && Date.parse(frmServerDate) < Date.parse(wkFridayDate)) &&
			(Date.parse(wkPrjStartDate) < Date.parse(selDate)) && (wkPrjEndDate == null || Date.parse(wkPrjEndDate) <= Date.parse(wkFridayDate)) &&
			Date.parse(frmServerDate) < Date.parse(wkPrjEndDate)) {
		return timesheetCannotSubmit();
	}

	if(((Date.parse(selDate) < Date.parse(frmServerDate)) && Date.parse(frmServerDate) < Date.parse(wkFridayDate)) &&
			(Date.parse(wkPrjStartDate) >= Date.parse(selDate) && Date.parse(wkPrjStartDate) < Date.parse(wkFridayDate)) &&
			Date.parse(frmServerDate) < Date.parse(wkFridayDate) ) {
		return timesheetCannotSubmit();
	}
	
	if(Date.parse(selDate) > Date.parse(frmServerDate)) {
		alert("Timesheet cannot be submitted for future dates");
		resetSubmit();
		submitOrNot = false;	
		return false;
	}
	
	if(selectedPM == -1 || selectedProjectTitle == -1){
		var message = "Please select valid";
		if(selectedPM == -1){
			message += " Project Manager";
		}
		
		if(selectedPM == -1 && selectedProjectTitle == -1){
			message += " and";
		}
		
		if(selectedProjectTitle == -1){
			message += " Project Title";
		}
		message += ".";
		alert(message);
		resetSubmit();
		submitOrNot = false;	
		return false;
		
	}

	if( submitOrNot ) {
		validateFormOnSubmit();
	}
}

function resetSubmit(){
	document.getElementById("Submit").disabled=false;
	document.getElementById("Submit").value="Submit";
}

function timesheetCannotSubmit() {
	alert("Timesheet cannot be submitted in the middle of a week");
	resetSubmit();
	submitOrNot = false;
	return false;
}

var xmlhttp;
var gcounter=-1;
var gloEmpCode=-1;
var delRowIdval = "";
var monId=0, tuesId=0, wedId=0, thursId=0,friId=0,satId=0, sunId=0;
function deleteRecordBasedOnCounter(counter,empCode, actualRowId)
{
	if(document.getElementById('deleteCounter') != null) {
	 	document.getElementById('deleteCounter').value = counter;
	}
	 gcounter = counter;
	 gloEmpCode = empCode;
	 
	 if(document.getElementById('monId'+gcounter) != null) {
		monId = document.getElementById('monId'+gcounter).value;
	 }
	 if(document.getElementById('tuesId'+gcounter) != null) {
		tuesId = document.getElementById('tuesId'+gcounter).value;
	 }
	 if(document.getElementById('wedId'+gcounter) != null) {
		wedId = document.getElementById('wedId'+gcounter).value;
	 }
	 if(document.getElementById('thursId'+gcounter) != null) {
		thursId = document.getElementById('thursId'+gcounter).value;
	 }
	 if(document.getElementById('friId'+gcounter) != null) {
		friId = document.getElementById('friId'+gcounter).value;
	 }
	 if(document.getElementById('satId'+gcounter) != null) {
		satId = document.getElementById('satId'+gcounter).value;
	 }
	 if(document.getElementById('sunId'+gcounter) != null) {
		sunId = document.getElementById('sunId'+gcounter).value;
	 }
	
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	 {
	  alert ("Your browser does not support AJAX!");
	  resetSubmit();
	  return;
	 }
	 var url="timesheet.html?method=deleteRecordTimesheet";
	 url=url+"&deleteCounter="+gcounter;
	 url=url+"&monId"+gcounter+"="+monId;
	 url=url+"&tuesId"+gcounter+"="+tuesId;
	 url=url+"&wedId"+gcounter+"="+wedId;
	 url=url+"&thursId"+gcounter+"="+thursId;
	 url=url+"&friId"+gcounter+"="+friId;
	 url=url+"&satId"+gcounter+"="+satId;
	 url=url+"&sunId"+gcounter+"="+sunId;
	 url=url+"&sid="+Math.random();
 	 gloCounter=counter-1; 
	 gloEmpCode=empCode;
	 delRowIdval = actualRowId;
	 xmlhttp.onreadystatechange=stateChanged;
	    
	 xmlhttp.open("GET",url,true);
	 xmlhttp.send(null);
	}

function stateChanged()
{ 
  if (xmlhttp.readyState==4) {
       try {
    	    var tble = document.getElementById("timesheetMainTable1");
    	    for(var i=0; i<rowCount; i++) {
    			var row = table.rows[i];
    			
    			if(null != row && row.id == delRowIdval) {
    				table.deleteRow(i);
    				break;
    			}
    		}
    	}catch(e) {
    		alert(e);
    		resetSubmit();
    	}
 	}
}
 
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

/*function deleteRecordBasedOnCounter(counter){	
	document.getElementById('deleteCounter').value = counter;
	document.timesheet.method ="POST";
	document.timesheet.action ="timesheet.html?method=deleteRecordTimesheet";
	document.forms.timesheet.submit();	
}*/
